﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QN_DiemSV.Forms
{
    public partial class frmGV : Form
    {
        public frmGV()
        {
            InitializeComponent();
        }
        bool them = false;
        bool sua = false;
        Classes.connect conn = new QN_DiemSV.Classes.connect();
        void VisionControl()
        {
            txtHoDem.Enabled = true;
            txtMaGV.Enabled = true;
            txtTen.Enabled = true;
            cboKhoa.Enabled = true;
        }
        void LoadToCombobox()
        {
            DataTable dt = conn.excuteSelect("select MaKhoa from KHOA");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cboKhoa.Items.Add(dt.Rows[i][0].ToString());
            }
        }
        void LoadData()
        {
            DataTable dt = new DataTable();
            dt = conn.excuteSelect("select * from GIAOVIEN");
            dgrDSGV.DataSource = dt;
        }

        private void frmGV_Load(object sender, EventArgs e)
        {
            txtTen.Enabled = false;
            txtHoDem.Enabled = false;
            txtMaGV.Enabled = false;
            cboKhoa.Enabled = false;
            LoadData();
            DataTable dt = conn.excuteSelect("select MaKhoa from KHOA");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            them = true;
            VisionControl();
            txtMaGV.Focus();
            LoadToCombobox();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            sua = true;
            VisionControl();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (them == true)
            {
                conn.EXE_PROC_PARAMS("GiaoVienInsert", "@MaGV", txtMaGV.Text, "@Hodem", txtHoDem.Text, "@Ten", txtTen.Text, "@MaK", cboKhoa.SelectedItem.ToString());
                LoadData();
            }
            if (sua == true)
            {
                conn.excuteNonquery("update GIAOVIEN set Ten =N'" + txtTen.Text + "', Hodem=N'" + txtHoDem.Text + "', MaKhoa='" + cboKhoa.Text + "' where MaGV='" + txtMaGV.Text.Trim() + "'");
                LoadData();
            }
        }

        private void dgrDSGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadToCombobox();
            int index = e.RowIndex;
            txtHoDem.Text = dgrDSGV.Rows[index].Cells["Hodem"].Value.ToString();
            txtMaGV.Text = dgrDSGV.Rows[index].Cells["MaGV"].Value.ToString();
            txtTen.Text = dgrDSGV.Rows[index].Cells["Ten"].Value.ToString();
            cboKhoa.Text = dgrDSGV.Rows[index].Cells["MaKhoa"].Value.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            conn.excuteNonquery("delete from GIAOVIEN where MaGV='" + txtMaGV.Text + "'");
            LoadData();
        }
    }
}
